//
//  ZSettingController.m
//  Zlottery
//
//  Created by HuaYingJiTuan on 16/1/11.
//  Copyright © 2016年 张庆庆. All rights reserved.
//

#import "ZSettingController.h"
#import "ZGroup.h"
#import "ZItem.h"
#import "ZItemArrow.h"
#import "ZItemSwitch.h"

#import "ZRedeemCodeController.h"
#import "ZMorePushController.h"
@interface ZSettingController ()
@property (nonatomic, strong) NSArray *groups;
@end

@implementation ZSettingController
//重写controller的init方法，设置tableView可以分组显示
- (instancetype)init{
    if (self = [super init]) {
        self = [super initWithStyle:UITableViewStyleGrouped];
    }
    return self;
}
//设置数据
- (NSArray *)groups{
    if (_groups == nil) {
        //获取数据
        //第一组
        ZItem *item11 = [ZItemArrow itemWithTitle:@"使用兑换码" icon:@"RedeemCode" destController:[ZRedeemCodeController class]];
        ZGroup *group1 = [ZGroup groupWithItems:@[item11]];
        
        //第二组
        ZItem *item21 = [ZItemArrow itemWithTitle:@"推送和提醒" icon:@"MorePush" destController:[ZMorePushController class]];
        ZItem *item22 = [ZItemSwitch itemWithTitle:@"摇一摇机选" icon:@"handShake"];
        ZItem *item23 = [ZItemSwitch itemWithTitle:@"声音效果" icon:@"sound_Effect"];
        ZItem *item24 = [ZItemSwitch itemWithTitle:@"购彩小助手" icon:@"More_LotteryRecommend"];
        ZItem *item25 = [ZItemSwitch itemWithTitle:@"圈子仅Wifi加载图片" icon:@"More_QuanZi_NetFlowSwitchImage"];
        ZGroup *group2 = [ZGroup groupWithItems:@[item21,item22,item23,item24,item25]];
        
        //第三组
        ZItem *item31 = [ZItemArrow itemWithTitle:@"检查新版本" icon:@"MoreUpdate" option:^{
            NSLog(@"检查新版本");
        }];
        ZItem *item32 = [ZItemArrow itemWithTitle:@"推荐给朋友" icon:@"MoreShare"];
        ZItem *item33 = [ZItemArrow itemWithTitle:@"产品推荐" icon:@"MoreNetease"];
        ZItem *item34 = [ZItemArrow itemWithTitle:@"关于" icon:@"MoreAbout"];
        ZGroup *group3 = [ZGroup groupWithItems:@[item31,item32,item33,item34]];
        _groups = @[group1,group2,group3];
    }
    return _groups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //第一个cell之前的间距
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0,15)];
    
    //设置每一组之间的间距
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 15;
    //设置tableView的背景图片
    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
}


#pragma mark - Table view data source
//tableView的代理方法，点击cell的时候执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //取出模型数据
    ZGroup *group = self.groups[indexPath.section];
    ZItem *item = group.items[indexPath.row];
    
    if (item.option) {
        item.option();
    }else if(item.destController){
        UIViewController *vc = [item.destController new];
        vc.title = item.title;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ZGroup *group = self.groups[section];
    return group.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //1 创建可重用的cell
    static NSString *reuseId = @"item";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    //2 给cell内部的属性赋值
    ZGroup *group = self.groups[indexPath.section];
    ZItem *item = group.items[indexPath.row];
    cell.textLabel.text = item.title;
    cell.imageView.image = [UIImage imageNamed:item.icon];
    
    if ([item isKindOfClass:[ZItemArrow class]]) {
        //箭头的cell
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if([item isKindOfClass:[ZItemSwitch class]]){
        //开关的cell
        //取消cell选中的样式
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryView = [UISwitch new];
    }else{
        cell.accessoryView = nil;
    }
    //3 返回cell
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
